package com.zhuoyue.biz.core.command.cmd.common.fac.base;

import com.zhuoyue.biz.core.api.domain.DeviceAndGatewayDO;
import com.zhuoyue.biz.core.api.dto.req.ControlReq;
import com.zhuoyue.common.constant.ControlType;
import com.zhuoyue.common.constant.DataBase;
import com.zhuoyue.common.utils.CmdUtils;

/**
 * 空调地暖设备数据帧基类
 * @author Administrator
 */
public class BaseCmd {
    public static String control(DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq) {
        String cmd = "";
        switch (controlReq.getControlType()) {
            //开
            case ControlType.TURN_ON_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, open());
                break;
            //关
            case ControlType.TURN_OFF_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, close());
                break;
            //锁定
            case ControlType.SET_LOCK_STATE_REQUEST:
                String lockState = controlReq.getActions().get("lockState") == null ? "" : (String) controlReq.getActions().get("lockState");
                cmd = getControlCmd(deviceAndGatewayDO, locking(lockState));
                break;
            //模式
            case ControlType.SET_MODE_REQUEST:
                String modeState = controlReq.getActions().get("modeState") == null ? "" : (String) controlReq.getActions().get("modeState");
                cmd = getControlCmd(deviceAndGatewayDO, mode(modeState));
                break;
            //风速
            case ControlType.INCREMENT_FAN_SPEED_REQUEST:
                String windRankState = controlReq.getActions().get("windRankState") == null ? "" : (String) controlReq.getActions().get("windRankState");
                cmd = getControlCmd(deviceAndGatewayDO, windRank(windRankState));
                break;
            //位置确认
            case ControlType.POSITION_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, position());
                break;
            default:
                break;


        }
        return cmd;
    }

    /**
     * 开
     *
     * @return
     */
    public static String open() {
        return IndexType0Fac.SWITCH + "01";
    }

    /**
     * 关
     *
     * @return
     */
    public static String close() {
        return IndexType0Fac.SWITCH + "02";
    }

    /**
     * 锁定
     *
     * @return
     */
    public static String locking(String lockState) {
        switch (lockState) {
            case ControlType.SetLocked.LOCKED:
                return IndexType0Fac.LOCKING + "02";
            case ControlType.SetLocked.UNLOCKED:
                return IndexType0Fac.LOCKING + "01";
            default:
                break;
        }
        return null;
    }

    /**
     * 模式
     *
     * @return
     */
    public static String mode(String modeState) {
        switch (modeState) {
            //制冷
            case ControlType.SetMode.COLD:
                return IndexType0Fac.MODE + "03";
            //制热
            case ControlType.SetMode.HEAT:
                return IndexType0Fac.MODE + "04";
            //送风
            case ControlType.SetMode.WIND:
                return IndexType0Fac.MODE + "05";
            default:
                break;
        }
        return null;
    }

    /**
     * 风速
     *
     * @return
     */
    public static String windRank(String windRankState) {
        switch (windRankState) {
            //低风速
            case ControlType.SetWindSpeed.LOW:
                return IndexType0Fac.WINDRANK + "02";
            //中风速
            case ControlType.SetWindSpeed.MID:
                return IndexType0Fac.WINDRANK + "03";
            //高风速
            case ControlType.SetWindSpeed.HIGH:
                return IndexType0Fac.WINDRANK + "04";
            //自动
            case ControlType.SetWindSpeed.AUTO:
                return IndexType0Fac.WINDRANK + "05";
            default:
                break;
        }
        return null;
    }




   /* //温度设置
    public static String temperature(String temperature) {

        return  IndexType_0FAC0202.TEMPERATURE + temperature;
    }*/

    /**
     * 位置确认
     *
     * @return
     */
    public static String position() {

        return IndexType0Fac.POSITION + "01";
    }


    public static String getControlCmd(DeviceAndGatewayDO deviceAndGatewayDO, String cmd10To12) {

        String cmd9 = CmdUtils.getDateLength(cmd10To12);

        String cmd3To12 = DataBase.WRITE +
                DataBase.ONE +
                deviceAndGatewayDO.getGatewayMac() +
                deviceAndGatewayDO.getZigbeeMac() +
                deviceAndGatewayDO.getOdIndex() +
                DataBase.VARIABLE_INDEX +
                cmd9 +
                cmd10To12;

        String cmd2 = CmdUtils.getDateLength(cmd3To12);

        String cmd13 = CmdUtils.getCmdBodySum(cmd3To12);

        String fullCmd = DataBase.DATA_HEAD +
                cmd2 +
                cmd3To12 +
                cmd13 +
                DataBase.DATA_END;

        return fullCmd;
    }


}
